﻿
package com.projects.xenia;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.projects.xenia.MainCustomersListActivity.EfficientAdapter.ViewHolder;
import com.projects.xenia.MainGoodsCatalogActivity.EfficientAdapter;
import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.ListActivity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;import android.graphics.Color;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainDebtsDetailsActivity extends ListActivity  {	
	
	private EfficientAdapter adap;
	
	private boolean isdialog;
	private String docid;
	private String datedoc;
	private String docnumber;
	private String doctype;	
	private String docacctype;
	private Double docsumm;
	private Double docrestsumm;
	private Double docpaydatecout;
	private Double docpaydaterest;
	
	private Cursor DebtsDetailsCursor;
	private Cursor AddDebtsDetailsCursor;
	
	private TextView DebtDetailsTotals;
	private ImageView _BackWardButton;
	
	private void GetDebtsAddDetailsCursor () {
		try {
			AddDebtsDetailsCursor = MainGlobalModule.DBE.rawQuery ("select descr,descr_add from xref_debts_details where docid = ? order by descr desc",new String [] {docid});
			} catch (Exception e) {
		}	
		
	}
	
	
	private void GetDebtsDetailsCursor () {
		
		try {
			//DebtsDetailsCursor = MainGlobalModule.DBE.rawQuery ("select distinct d.docid, d.docdate, d.docnum, t.descr , d.docpaydate, d.totalsum, d.restsum, strftime('%j',d.docpaydate)-strftime('%j',d.docdate), strftime('%j',d.docpaydate)-strftime('%j','now'), d.doctype, a.descr from xref_debts d left outer join xenum_typedoc t on d.doctype = t.id  left outer join xenum_AccType a on d.acctype = a.id where d.customers_id=? order by d.docdate asc",new String [] {MainGlobalModule.SelectedId});
			DebtsDetailsCursor = MainGlobalModule.DBE.rawQuery ("select distinct d.docid, d.docdate, d.docnum, t.descr , d.docpaydate, d.totalsum, d.restsum, julianday(date(d.docpaydate))-julianday(date(d.docdate)), julianday(date(d.docpaydate))-julianday(date('now')), d.doctype, a.descr from xref_debts d left outer join xenum_typedoc t on d.doctype = t.id  left outer join xenum_AccType a on d.acctype = a.id where d.customers_id=? order by d.docdate asc",new String [] {MainGlobalModule.SelectedId});
			
			
			} catch (Exception e) {
				e.printStackTrace();
		}		
		
	}
	
	private void GetDebtsDetailsCursor (String Firm, String Acctype, String Customer) {
		
		try {
			DebtsDetailsCursor = MainGlobalModule.DBE.rawQuery ("select distinct d.docid, d.docdate, d.docnum, t.descr , d.docpaydate, d.totalsum, d.restsum, julianday(date(d.docpaydate))-julianday(date(d.docdate)), julianday(date(d.docpaydate))-julianday(date('now')) , d.doctype, a.descr from xref_debts d left outer join xenum_typedoc t on d.doctype = t.id  left outer join xenum_AccType a on d.acctype = a.id where d.firms_id = ? and acctype = ? and d.customers_id=? order by d.docdate asc",new String [] {Firm,Acctype,Customer});
			} catch (Exception e) {
				e.printStackTrace();
		}		
		
	}
	
	private void GetDebtsDetailsCursor (String Customer) {
		
		try {
			DebtsDetailsCursor = MainGlobalModule.DBE.rawQuery ("select distinct d.docid, d.docdate, d.docnum, t.descr , d.docpaydate, d.totalsum, d.restsum, julianday(date(d.docpaydate))-julianday(date(d.docdate)), julianday(date(d.docpaydate))-julianday(date('now')) , d.doctype, a.descr from xref_debts d left outer join xenum_typedoc t on d.doctype = t.id left outer join xenum_AccType a on d.acctype = a.id where d.customers_id=? order by d.docdate asc",new String [] {Customer});
			} catch (Exception e) {
				e.printStackTrace();
		}		
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);            
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.debtsdetails_form);		
		
		_BackWardButton = (ImageView)findViewById(R.id.cust_backward);
		_BackWardButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		}); 
		
		Bundle DocExtra = getIntent().getExtras();
		
		if (DocExtra !=null) {
			isdialog = true;
			String Firm = DocExtra.getString("Firm");
			String AccType = DocExtra.getString("AccType");
			String Customer = DocExtra.getString("Customer");
			
			if ((Firm==null) & (AccType==null)) {
				GetDebtsDetailsCursor (Customer);				
			}
			else
			{
				GetDebtsDetailsCursor (Firm,AccType,Customer);
			}
						
			getListView().setOnItemClickListener(DebtsDetailsitem_Click);
		}
		else
		{
			isdialog = false;
			GetDebtsDetailsCursor ();			
		}
	
		DebtDetailsTotals = (TextView)findViewById(R.id.DebtDetailsTotals);
		
		Double TotalSum = 0.0;
		for (int i = 0;i<DebtsDetailsCursor.getCount();i++){
			DebtsDetailsCursor.moveToPosition(i);
			TotalSum += DebtsDetailsCursor.getDouble(6);
		}
		
		DebtDetailsTotals.setText(String.format("Всего: %.2f",TotalSum));		
		
		adap = new EfficientAdapter(this);
		setListAdapter(adap);
		
		MainGlobalModule.setBackGround(getListView());
		
	}	
	  
	private OnItemClickListener DebtsDetailsitem_Click = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {	
			
			DebtsDetailsCursor.moveToPosition(arg2);
			Intent DataIntent = new Intent ();
			
			DataIntent.putExtra("ddoc_id",DebtsDetailsCursor.getString(0));			
			DataIntent.putExtra("dtypedoc_id",DebtsDetailsCursor.getString(9));
			DataIntent.putExtra("dtypedoc_descr",DebtsDetailsCursor.getString(3));
			DataIntent.putExtra("ddocnum",DebtsDetailsCursor.getString(2));
			DataIntent.putExtra("ddocdate",DebtsDetailsCursor.getString(1));
			DataIntent.putExtra("dorder_sum",DebtsDetailsCursor.getDouble(5));
			DataIntent.putExtra("dpayment_sum",DebtsDetailsCursor.getDouble(6));
			
			setResult(RESULT_OK, DataIntent);
			finish();	
	        
		}
	};
	
	private class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
		 
		public EfficientAdapter(Context context) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		this.context = context;
		}
		 
		public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.debtsdetails_form_list, null);
		}
		
			holder = new ViewHolder();		
		
			holder.DebtsDetailsDateDoc = (TextView) convertView.findViewById(R.id.DebtsDetailsDateDoc); 
			holder.DebtsDetailsDocNumber = (TextView) convertView.findViewById(R.id.DebtsDetailsDocNumber);		
			holder.DebtsDetailsDocType = (TextView) convertView.findViewById(R.id.DebtsDetailsDocType);		
			holder.DebtsDetailsSum = (TextView) convertView.findViewById(R.id.DebtsDetailsSum);
			holder.DebtsDetailsRestSum = (TextView) convertView.findViewById(R.id.DebtsDetailsRestSum);
			holder.DebtsDetailsDescr = (TextView) convertView.findViewById(R.id.DebtsDetailsDescr); 
			
		
			getItem(position);
		
			
			holder.DebtsDetailsDateDoc.setText(datedoc); 
			holder.DebtsDetailsDocNumber.setText("№"+docnumber);
			holder.DebtsDetailsDocType.setText(doctype);		
			holder.DebtsDetailsSum.setText(String.format("%.2f", docsumm));
			holder.DebtsDetailsRestSum.setText(String.format("%.2f", docrestsumm));
						
			if (docpaydaterest<=0) {
				holder.DebtsDetailsRestSum.setTextColor(Color.RED);
			}
			else{
				holder.DebtsDetailsRestSum.setTextColor(Color.CYAN);
			}
				
			String addText = "";
			GetDebtsAddDetailsCursor ();
			for (int f = 0;f<AddDebtsDetailsCursor.getCount();f++) {
				AddDebtsDetailsCursor.moveToPosition(f);
				addText = addText.concat (String.format("%s%s ", AddDebtsDetailsCursor.getString(0),AddDebtsDetailsCursor.getString(1)));				
			}
			
			holder.DebtsDetailsDescr.setText(String.format("(%s) %s, %.0f/%.0f дн. ", docacctype,addText,docpaydatecout,docpaydaterest));
			
		return convertView;
		
		}
		 
		class ViewHolder {
		
			TextView DebtsDetailsDateDoc;		
			TextView DebtsDetailsDocNumber;
			TextView DebtsDetailsDocType;		
			TextView DebtsDetailsSum;		
			TextView DebtsDetailsRestSum;
			TextView DocPayDateCout;
			TextView DebtsDetailsDescr;
		
		}
		 				 
		@Override
		public long getItemId(int position) {

		return 0;
		}
		 
		@Override
		public int getCount() {

			return DebtsDetailsCursor.getCount();
		
		}
		 
		@Override
		public Object getItem(int position) {
			
			DebtsDetailsCursor.moveToPosition(position);
			
			docid= DebtsDetailsCursor.getString(0);
			datedoc = DebtsDetailsCursor.getString(1);
			docnumber = DebtsDetailsCursor.getString(2);			
			doctype = DebtsDetailsCursor.getString(3);
			docsumm = DebtsDetailsCursor.getDouble(5);			
			docrestsumm = DebtsDetailsCursor.getDouble(6);
			docpaydatecout = DebtsDetailsCursor.getDouble(7);
			docpaydaterest = DebtsDetailsCursor.getDouble(8);
			docacctype = DebtsDetailsCursor.getString(10);
			
		return docid;
		
		}
		
		public class OnItemClickListener implements OnClickListener {
			
		    private int mPosition;
		    OnItemClickListener(int position){
		        mPosition = position;		        
		    }
		    
		    @Override
		    public void onClick(View v) {	        			        	  
	        	  
		    }       
		    
		}	
}
	
	
}